package com.gitee.taotaojs.table;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import lombok.extern.java.Log;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author TaoTaojs
 * @date 2020/8/18 3:43 PM
 * <p>Description</p>
 * <h2>功能描述</h2>
 * mysql连接类
 * <h3>主要功能：</h3>
 * <ol>
 *     <li></li>
 * </ol>
 * <h3>关联类</h3>
 * <p>参考链接：</p>
 * <h2>更新日志</h2>
 * [author]        [time]                      [version]          [desc]
 * TaoTaojs        2020/8/18 3:43 PM             V1.0
 */
@Log
public final class MySqlConnection {

    private MySqlConnection(){}

    /** 连接对象 */
    private static Connection conn;

    /**
     * 初始化mysql数据源
     * @param dataSourceConfig 数据源配置
     */
    public static void initConnection(DataSourceConfig dataSourceConfig){
        //驱动程序名
        String driver = dataSourceConfig.getDriverName();
        //MySQL配置时的用户名
        String user = dataSourceConfig.getUsername();
        //MySQL配置时的密码
        String password = dataSourceConfig.getPassword();
        //URL指向要访问的数据库名mydata
        String url = dataSourceConfig.getUrl();

        try {
            Class.forName(driver);
            //声明Connection对象
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    public static List<String> getTableNames(String tableName){
        return getTableNames(tableName, null);
    }
    public static List<String> getTableNames(String tableName, DataSourceConfig dataSourceConfig){
        if(dataSourceConfig != null){
            initConnection(dataSourceConfig);
        }

        //存放所有表名
        List<String> tableNames = new ArrayList<>();
        try {
            //获取数据库的元数据
            DatabaseMetaData dbMetaData = conn.getMetaData();
            //从元数据中获取到所有的表名
            ResultSet rs = dbMetaData.getTables(null, null, null, new String[] {"TABLE"});
            while(rs.next()) {
//                System.out.println("表名: "+rs.getString("TABLE_NAME"));
//                System.out.println("表类型: "+rs.getString("TABLE_TYPE"));
//                System.out.println("表所属数据库: "+rs.getString("TABLE_CAT"));
//                System.out.println("表所属用户名: "+rs.getString("TABLE_SCHEM"));
//                System.out.println("表备注: "+rs.getString("REMARKS"));
                String tn = rs.getString("TABLE_NAME");
                if(tn.contains(tableName)){
                    tableNames.add(rs.getString("TABLE_NAME"));
                }
            }
        } catch (SQLException e) {
            log.info(e.getMessage());
        } finally {
            byDBclose();
        }
        return tableNames;
    }

    /** 清除数据库连接 */
    public static void byDBclose(){
        try {
            conn.close();
        } catch (SQLException e) {
            log.info("数据关闭异常：" + e.getMessage());
        }
    }


}
